package Window;
/*
 * Az osztály a hálózatbetöltő panelt hozza létre és tölti fel elemekkel.
 * */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import ModelAndController.Controller;

public class CreateCircuitPanel {
	
	//view peldany
	protected View view;
	//controller peldany
	protected Controller controller;
	//Legutobb hasznalt folder
	protected File dirOfLastUsedFolder;
	
	//Ablak meretei
	protected int panelWidth;
	protected int panelHeight;
	//panel
	protected JPanel createPanel;
	//elrendezes box
	protected Box horizontalBox;
	//Halozati file neve text fild
	protected JTextField circuitTextField;
	
	//Konstruktor
	public CreateCircuitPanel(View view, Controller controller, Dimension copyWindowSize, File copyDir){
		this.view = view;
		this.controller = controller;
		dirOfLastUsedFolder = copyDir;
		
		panelWidth = copyWindowSize.width;
		panelHeight = (int)(copyWindowSize.height*0.1);
		initPanelAndBox();
		initBoxElements();
		createBorderForPanel();
	}
	
	//Panel peldanyt adja vissza
	public JPanel getPanel(){
		return createPanel;
	}
	
	//panelt es boxot hozza letre
	private void initPanelAndBox(){
		BorderLayout border = new BorderLayout();
		createPanel = new JPanel(border);
		
		Dimension panelSize = new Dimension(panelWidth, panelHeight);
		createPanel.setMaximumSize(panelSize);
		createPanel.setPreferredSize(panelSize);
		
		horizontalBox = Box.createHorizontalBox();
	}
	
	//box tartalmanak letrehozasa
	private void initBoxElements(){
		//felirat
		JLabel loadCircuitLabel = new JLabel("Name of file containing circuit: ");
		horizontalBox.add(loadCircuitLabel);
		//input mező
		circuitTextField = new JTextField("");
		Dimension circuitTextSize = new Dimension( (int)(panelWidth*0.1), (int)(panelHeight*0.5));
		circuitTextField.setMinimumSize(circuitTextSize);
		circuitTextField.setPreferredSize(circuitTextSize);
		circuitTextField.setMaximumSize(circuitTextSize);
		horizontalBox.add(circuitTextField);
		//helykitöltő
		Dimension spaceBetweenTextAndButton = new Dimension( (int)(panelWidth*0.01), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceBetweenTextAndButton));
		//hálózatbetöltő gomb
		JButton newCircuitButton = new JButton("Load");
		ALForNewCircuitButton alForNewCiruitButton = new ALForNewCircuitButton();
		newCircuitButton.addActionListener(alForNewCiruitButton);
		horizontalBox.add(newCircuitButton);
		//helykitöltő
		Dimension spaceBetweenButtons = new Dimension( (int)(panelWidth*0.05), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceBetweenButtons));
		//böngésző gomb
		JButton circuitFromFolderButton = new JButton("Browse");
		ALForCircuitFromFolderButton alForCircuitFromFolderButton = new ALForCircuitFromFolderButton();
		circuitFromFolderButton.addActionListener(alForCircuitFromFolderButton);
		horizontalBox.add(circuitFromFolderButton);
		
		horizontalBox.add(Box.createHorizontalGlue());
	}
	
	//border letrehozas
	private void createBorderForPanel(){
		createPanel.add(horizontalBox);
		createPanel.setBorder(BorderFactory.createTitledBorder("Create or Load circuit"));
	}
	
	//textField alapjan betoltes
	private class ALForNewCircuitButton implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String fileName = circuitTextField.getText();
			controller.create(fileName);
		}
	}
	
	//folder bongeszes
	private class ALForCircuitFromFolderButton implements ActionListener{
		public void actionPerformed(ActionEvent e){
			JFileChooser jfc=new JFileChooser();
			jfc.setCurrentDirectory(dirOfLastUsedFolder);
			jfc.setDialogTitle("Hálózat rendszer megnyitása");
			int retValue=jfc.showOpenDialog(view.getFrame());
			if(retValue == 0){
				File getFile=jfc.getSelectedFile();
				view.setDirOfLastUsedFolder(jfc.getCurrentDirectory());
				controller.create(getFile.getPath());
			}
		}
	}
}
